<template>
  <a-modal
    title="未申请授权网关列表"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleCancel"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="0">
            <a-col :md="24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  style="float: right;margin-bottom: 16px;">
                  <a-button
                    class="btn_add"
                    type="primary"
                    @click="addGrant"
                  >批量申请授权</a-button>
                </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table
        ref="table"
        size="default"
        :columns="columns"
        :dataSource="data"
        :alert="true"
        :rowKey="(record) => record.id"
        id="table-wrapper"
        showPagination="auto"
        :pagination="false"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="grantStatus" slot-scope="text, record">
          <template v-if="text === '0'">待申请</template>
          <template v-if="text === '1'">待审核</template>
          <template v-if="text === '2'">审核通过</template>
          <template v-if="text === '3'">审核未通过</template>
          <template v-if="text === '4'">已过期</template>
        </span>
      </a-table>
    </a-spin>

    <grant-form ref="grantForm" @ok="handleOk"/>
  </a-modal>
</template>

<script>
  import { listNoGrant } from '@/api/modular/main/bizgateway/bizGatewayManage'
  import { STable } from '@/components'
  import grantForm from './grantForm'

  export default {
    components: {
      STable,
      grantForm
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        treeLoading: true,
        form: this.$form.createForm(this),
        grantInfo: {},
        gatewayCodes: null,
        selectedRowKeys: [],
        selectedRows: [],
        data: [],
        columns: [
          // 表头
          {
            title: '网关编码',
            dataIndex: 'gatewayCode'
          },
          {
            title: '授权状态',
            dataIndex: 'grantStatus',
            align: 'center',
            scopedSlots: { customRender: 'grantStatus' }
          },

          {
            title: '创建日期',
            dataIndex: 'createdAt'
          }
        ]
      }
    },
    mounted () {
      this.loadData()
    },
    methods: {
      // 加载数据
      loadData () {
        listNoGrant().then(res => {
          if (res.success) {
            this.data = res.data
          } else {
            this.$message.error('数据加载失败')
          }
        })
      },
      // 初始化方法
      init () {
        this.visible = true
        this.loadData()
      },
      // 多选事件
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      // 申报授权
      addGrant () {
        if (this.selectedRows.length <= 0) {
          this.$message.error('您没有选择网关')
          return
        }
        let gatewayCodes = []
        this.selectedRows.forEach(item => {
          gatewayCodes.push(item.gatewayCode)
        })
        this.$refs.grantForm.init(gatewayCodes)
      },
      handleOk () {
        this.loadData()
      },
      handleCancel () {
        this.visible = false
      }
    }
  }
</script>
